package cn.code.mycloud.module.system.controller.admin.aqi;

import cn.code.mycloud.framework.common.exception.ServiceException;
import cn.code.mycloud.framework.security.core.LoginUser;
import cn.code.mycloud.framework.security.core.util.SecurityFrameworkUtils;
import cn.code.mycloud.module.system.controller.admin.usergrid.vo.AssignUserGridRespVO;
import cn.code.mycloud.module.system.util.aqi.AQIUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.code.mycloud.framework.common.pojo.PageParam;
import cn.code.mycloud.framework.common.pojo.PageResult;
import cn.code.mycloud.framework.common.pojo.CommonResult;
import cn.code.mycloud.framework.common.util.object.BeanUtils;
import static cn.code.mycloud.framework.common.pojo.CommonResult.success;

import cn.code.mycloud.framework.excel.core.util.ExcelUtils;

import cn.code.mycloud.framework.apilog.core.annotation.ApiAccessLog;
import static cn.code.mycloud.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.code.mycloud.module.system.controller.admin.aqi.vo.*;
import cn.code.mycloud.module.system.dal.dataobject.aqi.AqiDO;
import cn.code.mycloud.module.system.service.aqi.AqiService;

@Tag(name = "管理后台 - AQI")
@RestController
@RequestMapping("/system/aqi")
@Validated
public class AqiController {

    @Resource
    private AqiService aqiService;

    @PostMapping("/create")
    @Operation(summary = "创建AQI")
    @PreAuthorize("@ss.hasPermission('system:aqi:create')")
    public CommonResult<Long> createAqi(@Valid @RequestBody AqiSaveReqVO createReqVO) {
        return success(aqiService.createAqi(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AQI")
    @PreAuthorize("@ss.hasPermission('system:aqi:update')")
    public CommonResult<Boolean> updateAqi(@Valid @RequestBody AqiSaveReqVO updateReqVO) {
        aqiService.updateAqi(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AQI")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:aqi:delete')")
    public CommonResult<Boolean> deleteAqi(@RequestParam("id") Long id) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        AqiDO aqi = aqiService.getAqi(id);
        if (user != null && user.getId().equals(aqi.getUserId())) {
            throw new ServiceException(1, "不能删除他人的AQI信息");
        }
        aqiService.deleteAqi(id);

        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AQI")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:aqi:query')")
    public CommonResult<AqiRespVO> getAqi(@RequestParam("id") Long id) {
        AqiDO aqi = aqiService.getAqi(id);
        return success(BeanUtils.toBean(aqi, AqiRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AQI分页")
    @PreAuthorize("@ss.hasPermission('system:aqi:query')")
    public CommonResult<PageResult<AqiRespVO>> getAqiPage(@Valid AqiPageReqVO pageReqVO) {
        PageResult<AqiDO> pageResult = aqiService.getAqiPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AqiRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AQI Excel")
    @PreAuthorize("@ss.hasPermission('system:aqi:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAqiExcel(@Valid AqiPageReqVO pageReqVO,
                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AqiDO> list = aqiService.getAqiPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AQI.xls", "数据", AqiRespVO.class,
                BeanUtils.toBean(list, AqiRespVO.class));
    }

    @Operation(summary = "指派网格用户")
    @PostMapping("/assign-grid-user")
    @PreAuthorize("@ss.hasPermission('system:aqi:assign-grid-user')")
    public CommonResult<Boolean> assignGridUser(@Valid @RequestBody AqiAssignReqVO aqiAssignReqVO) {
        aqiAssignReqVO.setStatus(1);
        aqiService.assignGridUser(aqiAssignReqVO);
        return success(true);
    }

    @PostMapping("/calculateAqi")
    @Operation(summary = "计算AQI")
    public CommonResult<AqiRespVO> calculateAqi(@Valid @RequestBody AqiCalculateVO calculateVO) {
        Map aqiByParam = AQIUtils.getAQIByParam(Double.valueOf(calculateVO.getSo2()), null, null, null, Double.valueOf(calculateVO.getPm25()), Double.valueOf(calculateVO.getCo()), AQIUtils.DAQI);
        AqiRespVO ret = new AqiRespVO();
        ret.setAqi((Integer) aqiByParam.get("aqi"));
        ret.setAqiLevel(AQIUtils.getAQILevel(ret.getAqi()));
        return success(ret);
    }

}